package com.mvc.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.model.test.face.UserService;
import com.model.test.model.User;

@Controller
public class AddController extends MultiActionController {
	
	@Autowired 
	private UserService userService;
	 
	protected final Log log = LogFactory.getLog(getClass());
	
	@RequestMapping(value = "/add")   
    public ModelAndView addMethod(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {  
		List<User> listUser= userService.getAllUserList();
		modelMap.put("listUser", listUser);
		return new ModelAndView("add", modelMap);
    }
	
	@RequestMapping(value = "/new", method = RequestMethod.GET)   
    public ModelAndView newMethod(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		modelMap.put("new", new User());
		return new ModelAndView("new", modelMap);
    }
	
	@RequestMapping(value = "/addCustomer/{uid}")
	@ResponseBody
    public String addCustomer(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,User user) throws Exception {
		userService.saveUser(user);
		Map map = new HashMap();
		map.put("statusCode", 200);
		map.put("message", "成功");
		map.put("navTabId", "");
		map.put("rel", "");
		map.put("callbackType", "closeCurrent");
		map.put("forwardUrl", "");
		String y = new String("我的心不乱".getBytes(),"utf-8"); ;
		String x = "{"
				+"\"statusCode\":\"200\","
				+"\"message\":\"乱\","
				+"\"navTabId\":\"\","
				+"\"rel\":\"\","
				+"\"callbackType\":\"closeCurrent\","
				+"\"forwardUrl\":\"\""
			+"}";
		
		HttpHeaders responseHeaders = new HttpHeaders();
	    responseHeaders.add("Content-Type", "text/html; charset=utf-8");

//	    return new ResponseEntity<String>(x, responseHeaders, HttpStatus.CREATED);

		return  x; 
//		return new ModelAndView("/success", modelMap);
    }
	
	@RequestMapping(value = "/modCustomer/{uid}")
	@ResponseBody
    public String modCustomer(@PathVariable Integer uid,HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		User user = userService.getUserById(uid);
		bind(request, user);
		userService.updateUser(user);
		Map map = new HashMap();
		map.put("statusCode", 200);
		map.put("message", "成功");
		map.put("navTabId", "");
		map.put("rel", "");
		map.put("callbackType", "closeCurrent");
		map.put("forwardUrl", "");
		String y = new String("我的心不乱".getBytes(),"utf-8"); ;
		String x = "{"
				+"\"statusCode\":\"200\","
				+"\"message\":\"乱\","
				+"\"navTabId\":\"\","
				+"\"rel\":\"\","
				+"\"callbackType\":\"closeCurrent\","
				+"\"forwardUrl\":\"\""
			+"}";
		
		HttpHeaders responseHeaders = new HttpHeaders();
	    responseHeaders.add("Content-Type", "text/html; charset=utf-8");

//	    return new ResponseEntity<String>(x, responseHeaders, HttpStatus.CREATED);

		return  x; 
//		return new ModelAndView("/success", modelMap);
    }
	
	@RequestMapping(value = "/del/{uid}")
    public ResponseEntity<String> delUser(@PathVariable Integer uid,HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		userService.deleteUser(uid);
		String x = "{"
				+"\"statusCode\":\"200\","
				+"\"message\":\"乱\","
				+"\"navTabId\":\"\","
				+"\"rel\":\"\","
				+"\"callbackType\":\"\","
				+"\"forwardUrl\":\"\""
			+"}";
		HttpHeaders responseHeaders = new HttpHeaders();
	    responseHeaders.add("Content-Type", "text/html; charset=utf-8");

	    return new ResponseEntity<String>(x, responseHeaders, HttpStatus.OK);

//		return  x; 
//		return new ModelAndView("/success", modelMap);
    }
	
	@RequestMapping(value = "/modify/{uid}")   
    public ModelAndView modi(@PathVariable Integer uid,HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		User user = userService.getUserById(uid);
		modelMap.put("user", user);
		return new ModelAndView("new", modelMap);
    }
}
